require('./user-login.css');

require('page/common/footer/footer.js');

require('page/common/nav-simple/nav-simple.js');

var _common_util  = require('util/common_util.js');
var _user_service = require('service/user-service.js');

// 错误信息显示
 var formError = {
    show    : function(errMsg){
        $('.error-item').show().find('.err-msg').text(errMsg);
    },
    hide    : function(){
        $('.error-item').hide().find('.err-msg').text('');
    }
 };

var page = {
    init : function(){
         this.bindEvent();
    },
    bindEvent : function(){
        var _this = this;
        // 登录按钮的点击
        $('#submit').click(function(){
            _this.submit();
        });
        // 如果按下回车，也进行提交
        $('.user-content').keyup(function(e){
            // keyCode == 13 表示回车键
            if(e.keyCode === 13){
                _this.submit();
            }
        });
    },
    submit : function(){
        var _this = this;
        var username = $.trim($('#username').val());
        var type = _this.loginType(username);
        if (! type){
            formError.show("请输入邮箱或手机号");
        }
        else{
            var formData = {
                value : username,
                password : $.trim($('#password').val()),
                type     : type
            };
    
            var validationResult = this.formValidation(formData);
            if (validationResult.status){
                
                //提交后端进行登录
                _user_service.login(formData, function(res){
                    console.log("返回成功");
                    sessionStorage.setItem('user_id', res.data.id);
                    sessionStorage.setItem('username', res.data.username);
                    // _common_util.setCookie("user_id",res.id, "1");
                    // _common_util.setCookie("username",res.username, "1");
                    // window.location.href = _common_util.getURLParam('redirect') || _common_util.getFrontURL('index.html');
                    window.location.href = _common_util.getFrontURL('index.html');
                },function(errMsg){
                    formError.show(errMsg);
                });
            }
            else{
                //提示数据
                formError.show(validationResult.msg);
            }
        }
    },
    loginType : function(username){
        if (/^1\d{10}$/.test(username)){
            return 'tel';
        }
        if (/^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/.test(username)){
            return 'mail';
        }
        return '';
    },
    formValidation : function(formData){
        var result = {
            status  :   false,
            msg     :   ''
        };
        
        //验证
        if (! _common_util.validate(formData.value, 'require')){
            result.msg = '用户名不能为空';
            return result;
        }

        if (! _common_util.validate(formData.password, 'require')){
            result.msg = '密码不能为空';
            return result;
        }
        //验证通过
        result.status = true;
        result.msg = '验证通过';
        return result;
    }
};

$(function(){
    page.init();
});
